<template>
    <el-container class="award-list">
        <el-main class="award-list__content">
            <div ref="container" class="table-container">


                <div class="award-list__header-main">

                    <el-select v-model="kindId" placeholder="请选择" @change="refresh" style="margin-right: 15px;">
                        <el-option v-for="item in kinds" :key="item.kindId" :label="item.kindTitle" :value="item.kindId">
                        </el-option>
                    </el-select>


                    <el-button size="small" type="primary" @click="openDialog('add')">添加</el-button>
                </div>
                <el-table ref="table" :data="datas" style="width: 100%" align="center" header-align="center">
                    <el-table-column label="名称" prop="name" />
                    <el-table-column label="头衔" prop="title" />
                    <el-table-column label="头像 / 图片">
                        <template slot-scope="scope">
                            <el-image :src="scope.row.avatar" style="width:100px"></el-image>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
        </el-main>

        <el-dialog :title="dialogText" :visible.sync="dialogVisible" width="400" center :lock-scroll="true" top="40px"
            :close-on-press-escape="false" :close-on-click-modal="false" v-if="dialogVisible">
            <el-form label-width="160px">
                <el-form-item label="名称" required>
                    <el-input v-model="formData.name" placeholder="名称" />
                </el-form-item>

                <el-form-item label="头衔" required>
                    <el-input v-model="formData.title" placeholder="头衔" />
                </el-form-item>

                <el-form-item label="头像 / 图片" required>
                    <upload-picture :url.sync="formData.avatar" prefix="award/assets">
                    </upload-picture>
                </el-form-item>
            </el-form>
            <span slot="footer">
                <el-button @click="cancel">取消</el-button>
                <el-button type="primary" @click="handleUpdate">确定</el-button>
            </span>
        </el-dialog>
    </el-container>
</template>

<script>
export default {
    name: "award-listList",

    data() {
        return {
            kinds: [],
            kindId: 1,
            datas: [],
            dialogVisible: false,
            dialogVisibleType: "",
            formData: {
                name: "",
                title: "",
                avatar: ""
            },
        };
    },

    computed: {
        dialogText() {
            return "添加";
        },
    },

    created() {
        this.getkinds();
        this.handleDatas();
    },
    methods: {
        async getkinds() {
            try {
                const { data } = await this.$fetch.post("/api/award-kind/list", {});
                this.kinds = data;
                this.kindId = data.find((item) => {
                    return item.isLatest
                }).kindId
            } catch (error) {
                console.log(error);
            }
        },
        async handleDatas() {
            const loading = this.$loading();
            try {
                const { data } = await this.$fetch.post("/api/award-list/list", { kindId: this.kindId });
                this.datas = data;
            } catch (error) {
                console.log(error);
            }
            loading.close();
        },
        refresh() {
            this.handleDatas();
        },

        openDialog() {
            this.dialogVisible = true;
            this.formData = {
                name: "",
                title: "",
                avatar: "",
                kindId: [this.kindId]
            }
        },

        async handleUpdate() {
            const params = {
                name: this.formData.name,
                title: this.formData.title,
                avatar: this.formData.avatar,
                kindIds: [this.kindId]
            };
            const { code } = await this.$fetch.post("/api/award-list/create", params);
            if (code === 0) {
                this.cancel();
            }

        },

        cancel() {
            this.dialogVisible = false;
            this.refresh();
        },

        handleDelete(row) {
            const params = {
                id: row.id,
            };
            this.$confirm("确定要删除？", "", {
                distinguishCancelAndClose: true,
                confirmButtonText: "确认",
                cancelButtonText: "取消",
            }).then(async () => {
                const { code } = await this.$fetch.post("/api/award-list/delete", params);
                if (code === 0) {
                    this.$message.success("删除成功");
                }
                this.refresh();
            });
        },
    },




};
</script>

<style scoped lang="scss">
.award-list {
    display: flex;
    flex-direction: column;
    height: calc(100vh - 50px);

    .award-list__header-main {
        height: 40px;
        display: flex;
        align-items: center;
        margin-bottom: 20px;
    }

    .award-list__content {
        display: flex;
        flex-direction: column;
        flex: 1;
        overflow: auto;
        padding-bottom: 0;

        .table-container {
            flex: 1;
            overflow: hidden;
            overflow-y: auto;
        }
    }

    .pagination-container {
        display: flex;
        justify-content: center;
        padding: 20px 0;

        .el-pagination {
            padding: 20px 0;
        }
    }
}

::v-deep .preview-image {
    width: 100px;
}
</style>
